package a09_贪心算法;

/**
 * <p>
 * a22_单调递增的数字复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/1/20
 */
public class a22_单调递增的数字复习1 {
    //332
    //从后往前先看32
    //变成了29
    //再看32
    //变成了299

    //1000
    //从后往前先看00
    //再看00
    //再看10
    //变成了09
    //最后900
    public static void main(String[] args) {
        a22_单调递增的数字复习1 test = new a22_单调递增的数字复习1();
        int i = test.monotoneIncreasingDigits(10);
        System.out.println();
    }
    public int monotoneIncreasingDigits(int n) {
        char[] chars = Integer.valueOf(n).toString().toCharArray();
        int flag = chars.length;
        for (int i = chars.length - 2; i >= 0; i--) {
            if(chars[i] > chars[i+1]){//前面的大于后面的
                chars[i] = (char)((Character.getNumericValue(chars[i]) - 1) + '0');
                flag = i + 1;
            }
        }
        for (int i = flag; i < chars.length; i++) {
            chars[i] = '9';
        }
        return Integer.valueOf(new String(chars));
    }
}
